import { Transaction } from "tiny-entity2";
import { IQueryObject } from "tiny-entity2/queryObject";
import { ServiceException } from "../common/excpetion";
import { ApiDataContext } from "../domain/apiDataContext";
import { AccountPermits } from "../domain/models/accountPermits";
import { PermitConfig } from "../domain/permitsConfig";
import { BaseService } from "./baseService";

export class AccountPermitsService extends BaseService {
    async CreateAccountPermits(entity: AccountPermits, ctx?: ApiDataContext) {
        ctx || (ctx = new ApiDataContext());
        return Transaction(ctx, async (ctx) => {
            let d = new AccountPermits();
            d.ConverToEntity(entity);
            return ctx.Create(d);
        });
    }
    async  UpdateAccountPermits(entity: AccountPermits, ctx?: ApiDataContext) {
        ctx || (ctx = new ApiDataContext());
        return Transaction(ctx, async (ctx) => {
            let d = new AccountPermits();
            d.ConverToEntity(entity);
            return ctx.Create(d);
        });
    }
    async DeleteAccountPermits(entity: AccountPermits, ctx?: ApiDataContext) {
        ctx || (ctx = new ApiDataContext());
        return Transaction(ctx, async (ctx) => {
            let d = new AccountPermits();
            d.ConverToEntity(entity);
            if(d.id)
            {
                return ctx.Delete(d);
            }
            else if(d.authObjectId && d.authObjectType && d.permitId)
            {
                return ctx.Delete<AccountPermits>(x=>x.authObjectId == $args1 && x.authObjectType==$args2 && x.permitId==$args3, ctx.AccountPermits,
                    {$args1:d.authObjectId,$args2:d.authObjectType,$args3:d.authObjectType}
                );
            }
            else if(d.authObjectId && d.authObjectType)
            {
                return ctx.Delete<AccountPermits>(x=>x.authObjectId == $args1 && x.authObjectType==$args2, ctx.AccountPermits,
                    {$args1:d.authObjectId,$args2:d.authObjectType}
                );
            }
            else if(d.permitId)
            {
                return ctx.Delete<AccountPermits>(x=>x.permitId == $args1, ctx.AccountPermits,{$args1:d.permitId});
            }
            else
            {
                throw new ServiceException("删除的参数不能为空","not_be_null");
            }
        });
    }
    async GetAccountPermitsById(id: string) {
        let ctx = new ApiDataContext();
        return ctx.AccountPermits.First(x => x.id == $args1, { $args1: id });
    }
   async QueryAccountPermits(params) {
        let ctx = new ApiDataContext();
        let q = ctx.AccountPermits as IQueryObject<AccountPermits>;
        if (params) {
            if (params.permitId) {
                q = q.Where(x => x.permitId == $args1, { $args1: params.permitId });
            }
            if (params.authObjectId) {
                q = q.Where(x => x.authObjectId == $args1, { $args1: params.authObjectId });
            }
            if (params.authObjectType) {
                q = q.Where(x => x.authObjectType >= $args1, { $args1: params.authObjectType });
            }
        }
        let list = await q.ToList();

        let permitConfig=PermitConfig;

        return {
            permitConfig,
            list
        };
    }
}